import 'package:data_acquisition_monitor/themes/default_theme.dart';
import 'package:flutter/material.dart';

import 'core/elegance_theme.dart';
import 'page/project.dart';
import 'page/acquisition_analysis.dart';
import 'view/header.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isDarkTheme = false;

  void toggleTheme() {
    setState(() {
      isDarkTheme = !isDarkTheme;
    });
  }

  @override
  Widget build(BuildContext context) {
    return EleganceTheme(
      data: isDarkTheme
          ? DefaultThemeImplementations.createDarkTheme()
          : DefaultThemeImplementations.createLightTheme(),
      child: MaterialApp(
        title: 'Elegance App',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
          useMaterial3: true,
          fontFamily: 'AlibabaPuHuiTi',
        ),
        darkTheme: ThemeData(
          colorScheme: ColorScheme.fromSeed(
            seedColor: Colors.blue,
            brightness: Brightness.dark,
          ),
          useMaterial3: true,
          fontFamily: 'AlibabaPuHuiTi',
          // Windows平台上的字体渲染优化
          platform: TargetPlatform.windows,
          // 启用抗锯齿和像素对齐
          visualDensity: VisualDensity.standard,
        ),
        themeMode: isDarkTheme ? ThemeMode.dark : ThemeMode.light,
        home: MyHomePage(
          title: 'Elegance Demo',
          onToggleTheme: toggleTheme,
          isDarkTheme: isDarkTheme,
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
    required this.onToggleTheme,
    required this.isDarkTheme,
  });

  final String title;
  final VoidCallback onToggleTheme;
  final bool isDarkTheme;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // 当前显示的内容ID
  String _currentContentId = 'project';
  
  // 当前工程名称
  String _currentProject = 'HIMENSEN-112';
  
  // 切换内容的方法
  void _switchContent(String contentId) {
    setState(() {
      _currentContentId = contentId;
    });
  }
  
  // 切换工程的方法
  void _switchProject() {
    // 这里可以实现弹出工程选择列表的逻辑
    // 目前简单地在控制台打印信息
    print('切换工程按钮被点击');
    // 实际应用中，这里可以显示一个工程选择对话框
  }
  
  // 根据内容ID获取对应的内容组件
  Widget _getContentWidget() {
    switch (_currentContentId) {
      case 'project':
        return ProjectPage(
          onEnterProject: () => _switchContent('acquisition_analysis'),
        );
      case 'settings':
        return Container(
          padding: EdgeInsets.all(16),
          child: Center(child: Text('工厂监控页面')),
        );
      case 'measure':
        return Container(
          padding: EdgeInsets.all(16),
          child: Center(child: Text('地震数据页面')),
        );
      case 'analysis':
        return Container(
          padding: EdgeInsets.all(16),
          child: Center(child: Text('告警管理页面')),
        );
      case 'acquisition_analysis':
        return const AcquisitionAnalysisPage();
      default:
        return const ProjectPage();
    }
  }
  
  @override
  Widget build(BuildContext context) {
    // 模拟数据 - 实际应用中应从数据源获取
    final now = DateTime.now();
    final formattedDateTime = 
        '${now.year}.${now.month.toString().padLeft(2, '0')}.${now.day.toString().padLeft(2, '0')} ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}';

    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            // 使用我们自定义的监控系统头部组件
            Header(
              isDarkTheme: widget.isDarkTheme,
              batteryPercentage: 92.1,
              currentDateTime: formattedDateTime,
              systemStatus: '2/10 AOR',
              alarmMessage: 'L480-04NALMN: Storage Barge Tank Level Low Low',
              alarmLevel: AlarmLevel.critical,
              currentProject: _currentProject,
              onSwitchProject: _switchProject,
            ),
            // 主要内容区域 - 自动填充剩余空间
            Expanded(
              child: Container(
                width: double.infinity,
                height: double.infinity,
                child: _getContentWidget(),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: null,
    );
  }
}
